package com.spring.advice;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.support.DelegatingIntroductionInterceptor;

public class ControllablePerformanceMonitor extends DelegatingIntroductionInterceptor implements MonitorAble {

    private ThreadLocal<Boolean> monitorStatusMap = new ThreadLocal<Boolean>();

    @Override
    public Object invoke(MethodInvocation mi) throws Throwable {

        //对于支持性能监视可控代理，通过判断其状态决定是否开启性能监视功能
        if(monitorStatusMap.get() != null && monitorStatusMap.get()){
            System.out.println("开始性能监视");
            super.invoke(mi);
            System.out.println("性能监视结束");
        }else{
            super.invoke(mi);
        }

        return null;
    }

    @Override
    public void setMonitorAble(Boolean b) {

        monitorStatusMap.set(b);
    }
}
